var twoSum = function (nums, target) {
  const hashMap = {};
  for (let i = 0; i < nums.length; i++) {
    const newNum = target - nums[i];
    const newNumIndex = hashMap[newNum];
    if (newNumIndex !== undefined) return [newNumIndex, i];
    hashMap[nums[i]] = i;
  }
};

let nums = [2, 11, 7, 15],
  target = 9;
let result = twoSum(nums, target);
console.log(result); //[0,2]
// 1. 用数组 a 表示数字的索引
// 2. 比如上面的例子中一直循环到 i=2 时，a[2] = 0, a[11] = 1, a[7] = 2
// 3. 此时 tmp = 2，a[2] 是存在的，所以返回 [0,2]
